<?php

namespace App\Exports;

use App\Models\Game\Center\Account;
use App\Models\Game\Center\Fish;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithCustomQuerySize;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class UserExport implements FromArray, WithHeadings, WithMapping, WithCustomQuerySize
{
    public $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * @return array
     */
    public function headings(): array
    {
        return [
            '玩家ID',
            '账号',
            '昵称',
            '渠道',
            'IMEI',
            'IDFA',
            '手机',
            '今日充值(元)',
            '累计充值(元)',
            '最后登录',
            '注册时间',
            '注册IP',
        ];
    }
/*
        'userId' => self::FIELD_TYPE_NUMERIC,
        'playerId' => self::FIELD_TYPE_NUMERIC,
        'username' => self::FIELD_TYPE_LIKE,
        'nickname' => self::FIELD_TYPE_LIKE,
        'mobile' => self::FIELD_TYPE_NUMERIC,
        'ip' => self::FIELD_TYPE_NORMAL,
        'idfa' => self::FIELD_TYPE_NORMAL,
        'imei' => self::FIELD_TYPE_NORMAL,
        'flagBit' => self::FIELD_TYPE_BIT,
        'source' => self::FIELD_TYPE_NUMERIC,
        'online' => self::FIELD_TYPE_NUMERIC,
        'created_at' => self::FIELD_TYPE_DATETIME_RANGE,
 * */
    /**
     * @return array
     */
    public function array(): array
    {
        $request = $this->request;
        return Account::ofFieldNumeric('userId', $request->userId)
            ->ofFieldNumeric('playerId', $request->playerId)
            ->ofLike('username', $request->username)
            ->ofLike('nickname', $request->nickname)
            ->ofFieldNumeric('mobile', $request->mobile)
            ->ofField('idfa', $request->idfa)
            ->ofField('imei', $request->imei)
            ->ofField('oaid', $request->oaid)
            ->ofField('uuid', $request->uuid)
            ->ofBit('flagBit', $request->flagBit)
            ->ofFieldNumeric('source', $request->source)
            ->ofFieldNumeric('online', $request->online)
            ->ofFieldNumeric('register_ip', ip2long($request->ip))
            ->ofDatetimeRange('created_at', $request->created_at)
            ->limit(1000)
            ->with('player')
            ->get()
            ->toArray();
    }

    public function querySize() :int
    {
        return 1000;
    }

    /**
     * @param Account $data
     * @return array
     */
    public function map($data): array
    {
        $arr = [
            $data['playerId'],
            $data['username'],
            $data['nickname'],
            $data['source'],
            $data['imei'],
            $data['idfa'],
            $data['mobile'],
            $data['userId'],
            empty($data['player']['sum_pay']) ? 0 : $data['player']['sum_pay']/100,
            $data['login_at'],
            $data['created_at'],
            $data['register_ip'],
        ];
        $arr = array_map(function($v) {
            return (string) $v;
        }, $arr);
        return $arr;
    }
}
